package com.example.demo.mapper;


import com.example.demo.model.Teacher;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;

@Mapper
public interface TeacherMapper {
    @Select("SELECT * FROM teacher")
    List<Teacher> findAll();

    @Select("SELECT * FROM teacher WHERE tel = #{tel}")
    List<Teacher> findByTel(String tel);

    @Select("SELECT * FROM teacher WHERE id = #{id}")
    Teacher get(Integer id);

    @Delete("DELETE FROM teacher WHERE id = #{id}")
    void delete(Integer id);

    @Insert("INSERT INTO teacher (name, age, tel) VALUES (#{name}, #{age}, #{tel})")
    void save(Teacher teacher);

    @Update("UPDATE teacher SET name = #{name}, age = #{age}, tel = #{tel} WHERE id = #{id}")
    void update(Teacher teacher);

    @Select("SELECT * FROM teacher WHERE " +
            "(#{teacher.name} IS NULL OR name LIKE '%' + #{teacher.name} + '%') AND " +
            "(#{teacher.age} IS NULL OR CAST(age AS VARCHAR) LIKE '%' + CAST(#{teacher.age} AS VARCHAR) + '%') AND " +
            "(#{teacher.tel} IS NULL OR tel LIKE '%' + #{teacher.tel} + '%')")
    List<Teacher> search(@Param("teacher") Teacher teacher);
    //List<Teacher> search(@Param("teacher") Teacher teacher);
}